var G_SEARCH_READY = 1;
var G_LAST_SEARCH_KEYWORDS = '';
var G_RESULT_COUNT = 0;
var G_SUGGESTION_CURSOR = 0;
var G_SEARCH_TYPE = 0;
var G_TRACTOR_HANDLE = 0;
var trigerObj;

function suggestRegTracker(){
	var vv = Utils.trim(trigerObj.value);
	if (vv == ''){
		G_LAST_SEARCH_KEYWORDS = '';
		close_suggestions();
		return false;
	}
	if (vv == G_LAST_SEARCH_KEYWORDS){
		return false;
	}
	if (G_SEARCH_READY == 0)
		return false;
	suggestSearch(vv);
}

function init_search_suggestion(triger,searchType){
	trigerObj = triger;
	G_SEARCH_TYPE = searchType;
	var parent = trigerObj.offsetParent;
	var x,y;
	x = trigerObj.offsetLeft;
	y = trigerObj.offsetTop;
	while (parent){
		x += parent.offsetLeft;
		y += parent.offsetTop;
		parent = parent.offsetParent;
	}
	y += 24;
	x += 'px';
	y += 'px';
	var suggestion_containter = document.getElementById('suggestion_containter');
	suggestion_containter.style.left = x;
	suggestion_containter.style.top = y;
	
	trigerObj.onfocus = function (){
		G_TRACTOR_HANDLE = setInterval("suggestRegTracker()",200);
		if (this.value =='输入您要找的商品名称或邮购号'){this.value =''};
	}
	trigerObj.onblur = function (){
		setTimeout("close_suggestions()", 500);
		if (this.value ==''){this.value ='输入您要找的商品名称或邮购号'};
	}
	trigerObj.onkeyup = function (event){
		event = window.event || event;
		switch(event.keyCode){
			case 13: //enter
				if (G_SEARCH_TYPE == 1){
					if (G_SUGGESTION_CURSOR > 0){
						G_SEARCH_READY = 0;
						trigerObj.value = document.getElementById('_ak_' + G_SUGGESTION_CURSOR.toString()).value;
					}
					document.forms.search_form.submit();
				}else if (G_SEARCH_TYPE == 2){
					if (G_SUGGESTION_CURSOR > 0){
						trigerObj.value = document.getElementById('_ak_' + G_SUGGESTION_CURSOR.toString()).value;
						close_suggestions();
					}else
						document.forms.search_form.submit();
				}
				return;
				break;
			case 27: //esc
				close_suggestions();
				return;
				break;
			case 37: //left
				return;
				break;
			case 39: //right
				return;
				break;
			case 38: //up
				var next_cursor;
				if (G_RESULT_COUNT > 0){
					if (G_SUGGESTION_CURSOR > 1){
						next_cursor = G_SUGGESTION_CURSOR - 1;
					}else if (G_SUGGESTION_CURSOR == 1 && G_RESULT_COUNT > 1){
						next_cursor = G_RESULT_COUNT;
					}else{
						break;
						return;
					}
					var next_sugg = document.getElementById('_ds_' + next_cursor.toString());
					if (next_sugg != null){
						if (G_SUGGESTION_CURSOR > 0){
		    				var current_sugg = document.getElementById('_ds_' + G_SUGGESTION_CURSOR.toString());
		    				current_sugg.style.backgroundColor = "";
							current_sugg.style.color="#000000";
						}
						next_sugg.style.backgroundColor="#ff6633";
						next_sugg.style.color="#ffffff";
					}
					G_SUGGESTION_CURSOR = next_cursor;
				}
				break;
			case 40: // down
				var next_cursor;
				if (G_RESULT_COUNT > 0){
					if (G_SUGGESTION_CURSOR < G_RESULT_COUNT){
						next_cursor = G_SUGGESTION_CURSOR + 1;
					}else if (G_SUGGESTION_CURSOR == G_RESULT_COUNT && G_RESULT_COUNT > 1){
						next_cursor = 1;
					}else{
						return;
						break;
					}
						
					var next_sugg = document.getElementById('_ds_' + next_cursor.toString());
					if (next_sugg != null){
						if (G_SUGGESTION_CURSOR > 0){
		    				var current_sugg = document.getElementById('_ds_' + G_SUGGESTION_CURSOR.toString());
		    				current_sugg.style.backgroundColor = "";
							current_sugg.style.color="#000000";
						}
						next_sugg.style.backgroundColor="#ff6633";
						next_sugg.style.color="#ffffff";
					}
					G_SUGGESTION_CURSOR = next_cursor;
				}
				break;
			default:
				if (G_TRACTOR_HANDLE == 0)
					G_TRACTOR_HANDLE = setInterval("suggestRegTracker()",200);
				break;
		}
	}
}

function suggestSearch(v){
	G_SEARCH_READY = 0;
	G_LAST_SEARCH_KEYWORDS = v;
	xajax_suggest_search(escape(v),G_SEARCH_TYPE);
}
function close_suggestions(){
	window.clearInterval(G_TRACTOR_HANDLE);
	G_TRACTOR_HANDLE = 0;
	G_SEARCH_READY = 1;
	G_RESULT_COUNT = 0;
	G_SUGGESTION_CURSOR = 0;
	G_LAST_SEARCH_KEYWORDS = '';
	document.getElementById('search_suggestion').innerHTML = '';
	document.getElementById('suggestion_containter').style.display = 'none';
}
function assign_keywords(id){
	trigerObj.value = document.getElementById('_ak_' + id.toString()).value;
	close_suggestions();
}
function suggestion_moveover(obj){
	if (G_SUGGESTION_CURSOR > 0){
		var last_obj = document.getElementById('_ds_' + G_SUGGESTION_CURSOR.toString());
		suggestion_moveout(last_obj);
	}
	obj.style.backgroundColor="#ff6633";
	obj.style.color="#ffffff";
	G_SUGGESTION_CURSOR = parseInt(obj.id.replace('_ds_',''));
}
function suggestion_moveout(obj){
	obj.style.backgroundColor = "";
	obj.style.color="#000000";
}